package ATM.controller;

import ATM.Service.ATMService;
import ATM.domain.Account;

import java.util.Random;
import java.util.Scanner;

public class LoginController {
    private Scanner sc = new Scanner(System.in);
    private Random ran = new Random();
    public void start(){
        while (true){
            System.out.println("========欢迎来到ATM系统========");
            System.out.println("1.用户登录");
            System.out.println("2.用户开户");
            String choice = sc.next();
            switch (choice){
                case "1":
                    Account act = login();
                    if(act != null){
                        ATMController atmController = new ATMController();
                        atmController.show(act);
                    }
                    break;
                case "2":
                    createAccount();
                    break;
                default:
                    System.out.println("您的输入有误，请重新输入！");
                    break;
            }
        }
    }

    private Account login() {
        ATMService atmService = new ATMService();
        Account loginAcc;

        System.out.println("请输入卡号：");
        String cardId = sc.next();
        Account account = atmService.findAccountByCardId(cardId);
        if(account == null){
            System.out.println("您输入的卡号不存在，请检查后再试");
            return null;
        }
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入密码：");
            String password = sc.next();

            if(password.equals(account.getPassword())){
                loginAcc = account;
                return loginAcc;
            }else {
                System.out.println("您输入的密码有误，请重新输入，还剩"+(2-i)+"次机会");
            }
        }
        System.out.println("今日机会已经用完，请下次再试。");
        return null;
    }

    private void createAccount() {
        Account account = new Account();
        System.out.println("请输入用户名：");
        String name = sc.next();
        account.setName(name);
        System.out.println("请输入性别：");
        while (true){
            String gender = sc.next();
            if("男".equals(gender)||"女".equals(gender)){
                account.setGender(gender);
                break;
            }else {
                System.out.println("您的输入有误，请输入男或女。");
            }
        }
        System.out.println("请输入密码：");
        String password = sc.next();
        System.out.println("请再次输入密码：");
        while (true){
            String rePwd = sc.next();
            if (password.equals(rePwd)) {
                account.setPassword(password);
                break;
            }else{
                System.out.println("您输入的密码有误，请重新输入！");
            }
        }
        System.out.println("请输入限额：");
        while (true){
            double limit = sc.nextDouble();
            if(limit > 0.0){
                account.setLimitMoney(limit);
                break;
            }else{
                System.out.println("输入的限额需要大于0，请重新输入！");
            }
        }

        String cardId = createCardId();
        account.setCardId(cardId);
        ATMService atmService = new ATMService();
        boolean flag = atmService.createAccount(account);

        if(flag){
            System.out.println("开户成功");
        }else {
            System.out.println("开户失败");
        }

    }

    private String createCardId() {
        while (true){
            String cardId = "";
            for (int i = 0; i < 8; i++) {
                int num = ran.nextInt(10);
                cardId += num;
            }

            ATMService atmService = new ATMService();
            Account account = atmService.findAccountByCardId(cardId);
            if(account == null){
                System.out.println(cardId);
                return cardId;
            }
        }
    }
}
